package com.sk.cardeal.act.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.ByExpendAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.ExpendListViewItemClickListener;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.ByKindList;
import com.sk.cardeal.entity.ByPartsEntity;
import com.sk.cardeal.entity.KmListEntitiy;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.view.MultiStateView;
import com.sk.cardeal.view.SimpleMultiStateView;
import com.sk.cardeal.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2018/4/13.
 */

public class NewChooseByPartsActivity extends BaseActivity implements HandleDataCallBack, ExpendListViewItemClickListener {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.expend_ll)
    ExpandableListView expendLl;
    @BindView(R.id.sure)
    TextView sure;
    @BindView(R.id.SimpleMultiStateView)
    SimpleMultiStateView simpleMultiStateView;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    private ArrayList<ByKindList.ResultListBean> allDataList = new ArrayList<>();
    KmListEntitiy kmListEntitiy;
    private String carCode = "";
    private String km = "";
    private ByExpendAdapter adapter;
    private ArrayList<ByKindList.ResultListBean> selectList = new ArrayList<>();

    @Override
    protected int getContentView() {
        return R.layout.activity_new_choose_by_parts;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setMyCallBack(this);

        initStateView();

        String data = getIntent().getStringExtra("data");
        carCode = getIntent().getStringExtra("carCode");
        km = getIntent().getStringExtra("km");
        kmListEntitiy = JSON.parseObject(data, KmListEntitiy.class);

        adapter = new ByExpendAdapter(allDataList, allDataList);
        expendLl.setAdapter(adapter);
        adapter.setExpendListViewItemClickListener(this);
        ArrayList<ByKindList.ResultListBean> dataList = getIntent().getParcelableArrayListExtra("list");
        if(dataList!=null&&dataList.size()>0){
            showSuccess();
            allDataList.addAll(dataList);
            adapter.notifyDataSetChanged();
            calculateTotal();
        }else {
            selectList = getIntent().getParcelableArrayListExtra("selectData");
            AppApi.getAllByKind(carCode, this, 0, this);
        }
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                finish();
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        showSuccess();
        ByKindList byKindList = JSON.parseObject(result, ByKindList.class);
        List<ByKindList.ResultListBean> dataList = byKindList.getResultList();
        ByKindList.ResultListBean dataBean = new ByKindList.ResultListBean();
        if (kmListEntitiy.getResultList() != null && kmListEntitiy.getResultList().size() > 0) {
            dataBean.setCarPartsList(kmListEntitiy.getResultList());
            dataBean.setId("0");
            dataBean.setPartsName(kmListEntitiy.getResultList().get(0).getMileage() + "公里维修保养服务");
            dataList.add(0, dataBean);
        }
        allDataList.addAll(dataList);

        for(int i = 0 ; i < allDataList.size() ; i++){
            for(int j = 0; j < selectList.size() ; j++){
                if(selectList.get(j).getId().equals(allDataList.get(i).getId())){{
                    allDataList.get(i).setCarPartsList(selectList.get(j).getCarPartsList());
                    allDataList.get(i).setChoose(true);
                }}
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {
    }

    @Override
    public void expendItemClickListener(View view, int groupPos, int childPos) {
        switch (view.getId()) {
            case R.id.change:
                Intent intent = new Intent(mContext, CanChangeListActivity.class);
                intent.putExtra("groupPos", groupPos);
                intent.putExtra("childPos", childPos);
                intent.putExtra("carCode", carCode);
                intent.putExtra("partsId", allDataList.get(groupPos).getCarPartsList().get(childPos).getPid());
                startActivityForResult(intent, 1);
                break;
            case R.id.add:
                int count = allDataList.get(groupPos).getCarPartsList().get(childPos).getSelectCount();
                allDataList.get(groupPos).getCarPartsList().get(childPos).setSelectCount(count + 1);
                adapter.notifyDataSetChanged();
                calculateTotal();
                break;
            case R.id.reduce:
                int count1 = allDataList.get(groupPos).getCarPartsList().get(childPos).getSelectCount();
                if (count1 >= 1) {
                    allDataList.get(groupPos).getCarPartsList().get(childPos).setSelectCount(count1 - 1);
                    adapter.notifyDataSetChanged();
                }
                calculateTotal();
                break;
            case R.id.icon:

                CheckBox checkBox = (CheckBox) view;
                if (checkBox.isChecked()) {
                    allDataList.get(groupPos).setChoose(true);
                } else {
                    allDataList.get(groupPos).setChoose(false);
                }
                adapter.notifyDataSetChanged();
                calculateTotal();
                break;

        }
    }

    private void calculateTotal() {

        double total = 0;
        int selectCount = 0;

        for (ByKindList.ResultListBean dataBean : allDataList) {
            if (dataBean.isChoose()) {
                selectCount++;
                for (ByPartsEntity entity : dataBean.getCarPartsList()) {
                    total = MyUtils.add(MyUtils.mul(Double.parseDouble(entity.getPrice()), entity.getSelectCount()), total);
                }
            }
        }

        tvTip.setText(selectCount + "项合计:");
        tvPrice.setText("￥" + total);

    }

    private void initStateView() {
        if (simpleMultiStateView == null) return;
        simpleMultiStateView.setEmptyResource(R.layout.view_empty)
                .setRetryResource(R.layout.view_retry)
                .setLoadingResource(R.layout.view_loading)
                .setNoNetResource(R.layout.view_nonet)
                .build()
                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
                    @Override
                    public void onReload() {
                    }
                });
    }

    public void showLoading() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showLoadingView();
        }
    }

    public void showSuccess() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showContent();
        }
    }

    public void showFaild() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showErrorView();
        }
    }

    public void showNoNet() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showNoNetView();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case 1:

                    int groupPos = data.getIntExtra("groupPos", 0);
                    int childPos = data.getIntExtra("childPos", 0);
                    ByPartsEntity entity = data.getParcelableExtra("data");
                    allDataList.get(groupPos).getCarPartsList().set(childPos, entity);
                    adapter.notifyDataSetChanged();
                    calculateTotal();

                    break;
            }
        }
    }

    @OnClick(R.id.sure)
    public void onViewClicked() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra("data",allDataList);
        setResult(RESULT_OK,intent);
        finish();
    }
}
